﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;
using BYFW.DAL;
using BYFW.SFL;
using LDSP.Util;
using LDSP.Helper;
using System.Data;

namespace LDSP.UI.Admin
{
    public partial class MngNews : LDSP.BRL.BaseValidUserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (this.RB())
                {
                    this.InitDataDynamicBefore();
                    if (!IsPostBack)
                    {
                        this.InitData();
                        this.BindGrid();
                    }
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        private List<int> PDFIDs = new List<int>();

        protected int col_index = 1;
        protected int col_name = 2;
        protected int col_publishDate = 3;
        protected int col_menu = 4;
        protected int col_comment = 5;
        protected int col_pdf = 6;
        protected int col_op = 7;

        /// <summary>
        /// 传入参数，设置过滤条件
        /// </summary>
        protected void SetSpecialPageInfo()
        {
            g_pageInfo.Direction = false;
            g_pageInfo.Sort = "publishDate";
            g_pageInfo.PageSize = GetPageSizeSetting();
            g_pageInfo.PageIndex = this.grd_list.PageIndex;
        }
        protected bool RB()
        {
            return true;
        }
        protected void InitData()
        {
            BindDDLPageSize();
            //不缓存则清空页面信息
            if (!needCachePageInfo)
            {
                g_pageInfo.ClearPageInfo();
            }
            this.SetSpecialPageInfo();
        }
        protected void BindDDLPageSize()
        {
            CommonControlInitial.InitialPageSize(this.ddl_pageSize);
            this.ddl_pageSize.SelectedValue = g_pageInfo.PageSize.ToString();
        }
        protected void InitDataDynamicBefore()
        {
            this.btn_add.Visible = IsSysAdmin;
            this.btn_delete.Visible = IsSysAdmin;
            this.btn_sendmail.Visible = IsSysAdmin;

            this.grd_list.Columns[0].Visible = IsSysAdmin;
        }
        protected void BindGrid()
        {
            //更新关键字表,以便智能提示.
            string keyword = this.txt_name.Text.Trim();
            if (!string.IsNullOrEmpty(keyword))
            {
                new SearchKeywords().IncreaseWordsCount(keyword);
            }

            DataTable dt = BYFW.SFL.CommonOP.SortedTable(new News().GetRecords(g_pageInfo.ConditionWhere, false), g_pageInfo.GetSortExpression());

            this.grd_list.PageSize = g_pageInfo.PageSize;
            this.grd_list.PageIndex = g_pageInfo.PageIndex;
            this.grd_list.DataSource = dt;
            this.grd_list.DataBind();

            SavePageSizeSetting();

            this.TotalRecords.Text = LDSP.Helper.StringUtil.GetSearchText(dt.Rows.Count);
        }

        protected void GetCondition()
        {
            string cond = "";
            string nameCond = this.txt_name.Text.Trim();
            if (nameCond != "")
            {
                string tempCond = this.AppendConditionMulti("", nameCond, "name", "menu", "comment");
                PDFIDs = new List<int>();
                if (chb_pdf.Checked)
                {
                    nameCond = UIHelper.CombindBlank(nameCond);
                    nameCond = nameCond.Replace(" ", " and ");
                    tempCond = this.AppendCondition(tempCond, string.Format("contains(pdfcontent,'{0}')", nameCond), false);
                    PDFIDs = UIHelper.GetPDFFilterIDsForNews(nameCond);
                }
                cond = this.AppendCondition(cond, tempCond, true);
                this.SetHighlight(nameCond);
            }

            if (this.txt_createStartDT.Text.Trim() != "")
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("publishDate", EDataType.Date, this.txt_createStartDT.Text, ">=", true), true);
            }
            if (this.txt_createEndDT.Text.Trim() != "")
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("createDate", EDataType.Date, this.txt_createEndDT.Text, "<=", true), true);
            }
            g_pageInfo.Condition = cond;
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                this.JSRedirect("../Admin/AddNews.aspx", false);
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_delete_Click(object sender, EventArgs e)
        {
            try
            {
                this.DeleteCheckedInGrid<News>(this.grd_list);
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_sendmail_Click(object sender, EventArgs e)
        {
            int id = 0;
            for (int i = 0; i <= grd_list.Rows.Count - 1; i++)
            {
                CheckBox chb = (CheckBox)grd_list.Rows[i].FindControl("chb");
                if (chb.Checked)
                {
                    News ett = new News();
                    ett.GetProperties(this.GetInt(this.grd_list.DataKeys[i].Value));
                    id = ett.ID;
                    break;
                }
            }

            if (id == 0)
            {
                this.JSAlert("请选择要发送的新闻！");
            }
            else
            {
                this.JSOpenWindow("EmailSender1.aspx?MailCodification=" + id + "&&type=News", 750, 650, null, null, null, 100, 0, false, false, false, false, false, false, true, false, false, false);
            }
        }
        protected void btn_search_Click(object sender, EventArgs e)
        {
            try
            {
                this.GetCondition();
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }


        protected void grd_list_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                int id = this.GetInt(e.CommandArgument.ToString());
                News ett = new News();
                ett.GetProperties(id);
                if (e.CommandName == "updateItem")
                {
                    this.JSRedirect("../Admin/AddNews.aspx?id=" + id, false);
                }
                else if (e.CommandName == "deleteItem")
                {
                    ett.Delete();
                }
                else if (e.CommandName == "downloadItem")
                {
                    this.StartDownload(ett.ID, DocType.新闻);
                }
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    News ett = new News();
                    ett.GetProperties(((DataRowView)e.Row.DataItem).Row);
                    this.ShowSequenceNumber(e.Row, col_index);

                    e.Row.Cells[col_publishDate].Text = this.FormatDT(ett.PublishDate);

                    //获取快照。
                    e.Row.Cells[col_menu].Text = this.GetSnapshot(ett.Menu);
                    this.HighlightMulti(e.Row, col_name, col_menu, col_comment);
                    e.Row.Cells[col_menu].Text = UIHelper.AppendLink(e.Row.Cells[col_menu].Text, LDSP.Helper.LongTextInstanceNames.News, Server.UrlEncode(this.GetHighlight()), ett.ID);

                    UIHelper.AppendEmptyLine(e.Row, col_menu);
                    UIHelper.EnableShowEnter(e.Row, col_menu);

                    //如果是企业用户，隐藏"修改"和"删除"按钮
                    if (LoginAdmin == null)
                    {
                        WebControl lnk_Modify = (WebControl)e.Row.FindControl("LinkButton2");
                        WebControl lnk_Delete = (WebControl)e.Row.FindControl("LinkButton3");
                        lnk_Modify.Visible = false;
                        lnk_Delete.Visible = false;
                    }

                    if (PDFIDs.Contains(ett.ID))
                    {
                        e.Row.Cells[col_pdf].Text = "包含";
                    }
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                g_pageInfo.PageIndex = e.NewPageIndex;
                this.GetCondition();
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                if (g_pageInfo.Sort == e.SortExpression)
                {
                    g_pageInfo.Direction = !g_pageInfo.Direction;
                }
                else
                {
                    g_pageInfo.Direction = true;
                }
                g_pageInfo.Sort = e.SortExpression;
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void ddl_pageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                g_pageInfo.PageSize = this.GetInt(this.ddl_pageSize.SelectedValue);
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
    }

}